﻿using WRJClient.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace WRJClient.Common
{
    public class DataChangedEventArgs : EventArgs
    {
        public object Data;
    }
    public abstract class DataManager<T> : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public delegate void OnDataAddedHandler(object sender, DataChangedEventArgs e);
        public event OnDataAddedHandler OnDataAdded;
        public delegate void OnDataRemovedHandler(object sender, DataChangedEventArgs e);
        public event OnDataRemovedHandler OnDataRemoved;

        protected ObservableCollection<T> _datas = new ObservableCollection<T>();
        public ObservableCollection<T> Datas { get { return _datas; } }

        public abstract T Get(string dbId);

        public void Add(T t)
        {
            if (addChild(t))
            {
                DataChangedEventArgs e = new DataChangedEventArgs { Data = t };
                OnDataAdded?.Invoke(this, e);
            }
        }
        protected abstract bool addChild(T t);

        public void Remove(T t)
        {
            if (removeChild(t))
            {
                DataChangedEventArgs e = new DataChangedEventArgs { Data = t };
                OnDataRemoved?.Invoke(this, e);
            }
        }
        protected abstract bool removeChild(T t);
        public void RemoveAll()
        {
            _datas.Clear();
        }

    }
}
